/*
 * @Author: 陈德基
 * @Date: 2021-11-15 17:26:29
 * @LastEditTime: 2021-11-15 17:34:02
 * @Description: 
 * @FilePath: \copy_gsy_github_app_flutter\lib\db\sql_manager.dart
 * 联系方式：
 * wx: melody_2009
 * qq: 7815171323 
 */

import 'dart:io';

import 'package:sqflite/sqflite.dart';

class SqlDriver {
  static const _v = 1;
  static const _dbName = 'copy_gsy_github_app_flutter.db';
  static Database? _database;

  static init() async {
    var dbPath = await getDatabasesPath();
    var dbName = _dbName;
    if (dbPath != null) {
      String path = dbPath + dbName;
      if (Platform.isIOS) {
        path = dbPath + "/" + dbName;
      }
      _database = await openDatabase(path, version: _v, onCreate: (db, ver) {});
    }
  }

  /**
   * 表是否存在
   */
  static isTableExits(String tableName) async {
    await getCurrentDatabase();
    var res = await _database?.rawQuery(
        "select * from Sqlite_master where type = 'table' and name = '$tableName'");
    return res != null && res.length > 0;
  }

  ///获取当前数据库对象
  static Future<Database?> getCurrentDatabase() async {
    if (_database == null) {
      await init();
    }
    return _database;
  }

  ///关闭
  static close() {
    _database?.close();
    _database = null;
  }
}
